
#^断参
def Inspect(elements:list=[],json_data:dict={})->(bool,str):
    if not elements:
        return False,"判定参数不能为空"
    list_=list()
    list_.extend(json_data)
    for element in elements:
        if type(element)==list :
            if not element[0] in list_:
                return False,f"\"{element[0]}\"参数缺失2"
            for i in range(len(element)-1):
                if type(json_data[element[0]])==element[i+1]:
                    break
            else:
                print(type(json_data[element[0]]),element[0])
                s="".join(str(i) for i in element[1:])
                return False,f"\"{element[0]}\"类型应为->{s}"
            continue
        if not element in list_:
                return False,f"\"{element}\"参数缺失1"
    return True,None

#^生成token
import jwt
import time
def generateToken(user,key,**kwargs):
    payload=dict()
    payload["UserID"]=user['UserId']
    payload["UserName"]=user['UserName']
    
    for i in kwargs:
        payload[i]=kwargs[i]
    return jwt.encode(payload, key, algorithm="HS256")
#^生解析token
def verify_jwt_token(key, token: str)->(bool,dict):
    """验证用户token"""
    try:
        payload = jwt.decode(token, key, algorithms=["HS256"])
    except jwt.PyJWTError:
        print('token解析失败')
        return False,{"UserID":"","UserName":""}#^没有登录则数据为空
    else:
        exp = int(payload.pop('exp'))
        if time.time() > exp:
            print('已失效')
            return False,{"UserID":"","UserName":""}#^token过期则数据为空
        print(payload)
        return True,payload

#^发送验证
from django.core.mail import send_mail
def send_sms_code(to_email,message,html_message):
    """
    发送邮箱验证码
    :param to_mail: 发到这个邮箱
    :return: 成功：0 失败 -1
    """
    # 生成邮箱验证码
    # sms_code = '%06d' % random.randint(0, 999999)
    EMAIL_FROM = "纸上花园<3394954185@qq.com>"  # 邮箱来自
    email_title = '【纸上花园-邮箱验证】'
    html_message = html_message
    send_status = send_mail(subject=email_title,message=message,html_message= html_message,from_email= EMAIL_FROM,recipient_list= to_email)
    return send_status

